package cn.wolfcode.wolf2w.business.controller;
import cn.wolfcode.wolf2w.business.api.domain.Note;
import cn.wolfcode.wolf2w.business.api.domain.NoteContent;
import cn.wolfcode.wolf2w.business.api.domain.NoteEs;
import cn.wolfcode.wolf2w.business.api.RemoteNoteEsService;
import cn.wolfcode.wolf2w.business.service.INoteContentService;
import cn.wolfcode.wolf2w.common.core.constant.SecurityConstants;
import cn.wolfcode.wolf2w.common.core.domain.R;
import cn.wolfcode.wolf2w.common.security.annotation.InnerAuth;
import cn.wolfcode.wolf2w.business.query.NoteQuery;
import cn.wolfcode.wolf2w.business.service.INoteService;
import cn.wolfcode.wolf2w.member.api.RemoteUserInfoService;
import cn.wolfcode.wolf2w.member.api.domain.UserInfo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 旅游日记 Controller
 * 
 * @author Tianluhuan
 * @date 2025-09-16
 */
@RestController
@RequestMapping("notes")
public class NoteController {
    @Autowired
    private INoteService noteService;
    @Autowired
    private RemoteNoteEsService remoteNoteEsService;
    @Autowired
    private RemoteUserInfoService userInfoService;
    @Autowired
    private INoteContentService noteContentService;


    @PostMapping("/add")
    public R<?> add(@RequestBody Note note) {
        noteService.insert(note);
        return R.ok();
    }

    @GetMapping("/dataInit")
    public R<?> dataInit(){
        List<Note> list = noteService.list();
        for (Note note : list) {
            NoteEs es = new NoteEs();
            //同名属性拷贝，把note对象中的数据拷贝到es中
            BeanUtils.copyProperties(note, es);
            remoteNoteEsService.initNoteEs(es, SecurityConstants.INNER);
        }
        return R.ok("游记数据初始化完成");
    }
    
    /**
     * 旅游日记详情
     */
    @GetMapping("/detail/{id}")
    public R<Note> detail(@PathVariable Long id) {
        Note note = noteService.getById(id);
        R<UserInfo> one = userInfoService.getOne(id,SecurityConstants.INNER);
        NoteContent byId = noteContentService.getById(id);
        note.setContent(byId);
        note.setAuthor(one.getData());
        return R.ok(note);
    }
    /**
     * 旅游日记 列表
     */
    @GetMapping("/query")
    public R<IPage<Note>> query(NoteQuery qo) {
        IPage<Note> page = noteService.queryPage(qo);
        return R.ok(page);
    }

    @GetMapping("/viewnnumTop3")
    public R<List<Note>> viewnnumTop3(Long destId) {
        List<Note> list= noteService.queryviewnnumTop3(destId);
        return R.ok(list);
    }

    @PostMapping("/collect/{id}")
    public R<Map<String,Object>> collect(@PathVariable Long id){
        Map<String,Object> map = noteService.collect(id);
        return R.ok(map);
    }
    @GetMapping("/isUserFavor")
    public R<Boolean> isUserFavor(Long uid,Long sid) {
        Boolean b= noteService.isUserFavor(uid,sid);
        return R.ok(b);
    }

    @PostMapping("/star/{id}")
    public R<Map<String,Object>> star(@PathVariable Long id){
            Map<String,Object>  map = noteService.star(id);
            return R.ok(map);
    }
    @PostMapping("/viewnumIncr")
    public R<Map<String, Object>> viewnumIncr(Long nid) {
        Map<String, Object> map = noteService.viewnumIncr(nid);
        return R.ok(map);
    }

    /*****************************************对外暴露Fegin接口**********************************************/
    /**
     * Feign 接口
     */
    @GetMapping("/feign/list")
    public R<List<Note>> feignList() {
        return R.ok(noteService.list());
    }
    @InnerAuth
    @GetMapping("/feign/{id}")
    public R<Note> feignGet(@PathVariable Long id) {
        return R.ok(noteService.getById(id));
    }

    @PostMapping("/feign/noteStatisHashPersistence")
    public R<?> noteStatisHashPersistence(){
        noteService.noteStatisHashPersistence();
        return R.ok();
    }
}
